﻿using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    public class ClientBusiness
    {
        /// <summary>
        /// 根据Guuid获取Client信息
        /// </summary>
        /// <param name="guuid"></param>
        /// <returns></returns>
        public static VClientEntity GetClient(string guuid)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {

                var cli = from client in entities.Client
                          where client.Guuid == guuid && client.IsActive == true
                          select new VClientEntity
                          {
                              ID = client.ID,
                              CropID = client.CropID,
                              APPID = client.APPID,
                              Guuid = client.Guuid,
                              ClientName = client.ClientName,
                              Secret = client.Secret,
                          };
                return cli.FirstOrDefault();
            }
        }
        /// <summary>
        /// 根据clientID获取Client
        /// </summary>
        /// <param name="clientID"></param>
        /// <returns></returns>
        public static VClientEntity GetClient(long clientID)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {

                var cli = from client in entities.Client
                          where client.ID == clientID && client.IsActive == true
                          select new VClientEntity
                          {
                              ID = client.ID,
                              CropID = client.CropID,
                              APPID = client.APPID,
                              Guuid = client.Guuid,
                              ClientName = client.ClientName,
                              Secret = client.Secret,
                          };
                return cli.FirstOrDefault();
            }
        }
        /// <summary>
        /// 根据用户ID获取client
        /// </summary>
        /// <param name="endUserID"></param>
        /// <returns></returns>
        public static VClientEntity GetClientByEndUserID(long endUserID)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                var cli = from client in entities.Client
                          join enduser in entities.EndUser
                          on client.ID equals enduser.ClientID
                          where enduser.ID == endUserID && (enduser.IsActive == true) && (client.IsActive == true)
                          select new VClientEntity
                          {
                              APPID = client.APPID,
                              ClientName = client.ClientName,
                              CropID = client.CropID,
                              Guuid = client.Guuid,
                              ID = client.ID,
                              Secret = client.Secret,
                              IsActive = client.IsActive
                          };
                return cli.FirstOrDefault();
            }
        }
        /// <summary>
        /// 添加一个Client
        /// </summary>
        /// <param name="client"></param>
        public static long AddClient(VClientEntity client)
        {
            using (CustomerServiceCenterEntities entities = new CustomerServiceCenterEntities())
            {
                Client cli = new Client
                {
                    APPID = client.APPID,
                    CropID = client.CropID,
                    Secret = client.Secret,
                    ClientName = client.ClientName,
                    Guuid = client.Guuid,
                    IsActive = true
                };
                entities.Client.Add(cli);
                entities.SaveChanges();
                return cli.ID;
            }
        }
    }
}
